package com.fanshuai.spring.boot.controller;

import com.fanshuai.spring.boot.domain.BookMsg;
import com.fanshuai.spring.boot.domain.UserMsg;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class KafkaTestController {
    ObjectMapper mapper = new ObjectMapper();

    @Resource
    private KafkaTemplate kafkaTemplate;

    @GetMapping("/book/send")
    public String sendBookMessage(long id) throws Exception {
        BookMsg msg = BookMsg.builder().id(id).name("book").build();
        kafkaTemplate.send("book-test", "key", mapper.writeValueAsString(msg));
        return "success";
    }

    @GetMapping("/user/send")
    public String sendUserMessage(long id) throws Exception {
        UserMsg msg = UserMsg.builder().id(id).nickName("user").gender("女").build();
        kafkaTemplate.send("user-test", "key", mapper.writeValueAsString(msg));
        return "success";
    }
}
